package com.ke.jun.view.widght

import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.View
import androidx.core.graphics.withSave
import com.ke.jun.view.R

@SuppressLint("ResourceType")
class GlanceView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val gradient = GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM,
        intArrayOf(Color.TRANSPARENT, Color.BLUE)
    )

    /**
     * create by iso88591 at 2021/8/11 🙃
     * 配置 圆环的宽度
     */
    private var ringWidth = 20

    /** 扫描的高度比 **/
    private val gradientPercent = 0.25f
    var offset = 0f
        set(value) {
            field = value
            invalidate()
        }


    /** 半径 **/
    private var radius: Float = 0f
    private val roundPath = Path()

    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        this.strokeWidth = ringWidth.toFloat()
        this.style = Paint.Style.STROKE
    }

    init {

        val arr = context.obtainStyledAttributes(
            attrs, intArrayOf(
                R.attr.ringWidth,
                R.attr.scanningRadius
            ), defStyleAttr, 0
        )

        ringWidth = arr.getDimension(0,10f).toInt()
        radius = arr.getDimension(1, 100f)

        arr.recycle()

        /**
         * create by iso88591 at 2021/8/11 🙃
         * 扫描动画
         *
         */
        val ani = ObjectAnimator.ofFloat(
            this,
            "offset",
            0.2f,
            0.75f
        )
        ani.repeatCount = -1
        ani.duration = 800
        ani.startDelay = 200
        ani.start()

    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)

        roundPath.addCircle(width / 2f, height / 2f, radius-ringWidth/2f, Path.Direction.CW)

        /** 配置着色器 **/
        mPaint.shader = LinearGradient(
            width / 2f,
            0f,
            width / 2f,
            height.toFloat(),
            Color.parseColor("#330EC875"),
            Color.parseColor("#CC09BAD4"),
            Shader.TileMode.CLAMP
        )

    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        mPaint.strokeWidth = width.toFloat()
        canvas.withSave {
            clipPath(roundPath,Region.Op.DIFFERENCE)
            canvas.drawLine(width/2f,0f,width/2f,height.toFloat(),mPaint)
        }

        gradient.setBounds(
            ringWidth, ringWidth, width - ringWidth, height - ringWidth
        )

        canvas.withSave {
            clipPath(roundPath)
            translate(0f, offset * height)
            clipRect(
                0f,
                0f,
                width.toFloat(),
                height * gradientPercent
            )
            gradient.draw(canvas)

        }

        mPaint.strokeWidth = ringWidth.toFloat()
        canvas.drawCircle(width / 2f, height / 2f, radius, mPaint)

    }
}